package com.hyphenate.easeui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.hyphenate.easeui.R;

public class EaseChatVoiceMenu extends LinearLayout {

    private static final String TAG = "EaseChatVoiceMenu";

    public EaseChatVoiceMenu(Context context) {
        super(context);
        init(context);
    }

    public EaseChatVoiceMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public EaseChatVoiceMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public EaseChatVoiceMenu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    @SuppressLint("ClickableViewAccessibility")
    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.ease_widget_chat_voice, this);
        ViewGroup containerVoice = view.findViewById(R.id.container_voice);
        ImageView ivVoice = view.findViewById(R.id.iv_voice);
        TextView tvMsg = view.findViewById(R.id.tv_msg);

        containerVoice.setOnTouchListener((view1, motionEvent) -> {
            if (mListener != null) mListener.onPressToSpeakBtnTouch(view1, motionEvent);
            int x = (int) motionEvent.getRawX();
            int y = (int) motionEvent.getRawY();
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    containerVoice.setBackgroundResource(R.drawable.shape_voice_bg_pressed);
                    tvMsg.setText("正在录音...");
                    break;
                case MotionEvent.ACTION_UP:
                    containerVoice.setBackgroundResource(R.drawable.shape_voice_bg_normal);
                    tvMsg.setText("按下开始录音");
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (isTouchPointView(containerVoice, x, y)) {
                        containerVoice.setBackgroundResource(R.drawable.shape_voice_bg_pressed);
                        tvMsg.setText("正在录音...");
                    } else {
                        containerVoice.setBackgroundResource(R.drawable.shape_voice_bg_cancel);
                        tvMsg.setText("松开取消");
                    }
                    break;
            }
            return true;
        });
    }

    private boolean isTouchPointView(View view, int x, int y) {
        if (view == null) {
            return false;
        }
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int left = location[0];
        int top = location[1];
        int right = left + view.getMeasuredWidth();
        int bottom = top + view.getMeasuredHeight();
        //view.isClickable() &&
        if (y >= top && y <= bottom && x >= left
                && x <= right) {
            return true;
        }
        return false;
    }

    private OnSendVoiceListener mListener;

    public void setOnSendVoidListener(OnSendVoiceListener listener) {
        mListener = listener;
    }

    public interface OnSendVoiceListener {
        void onPressToSpeakBtnTouch(View v, MotionEvent event);
    }

}
